/**
 * 项目名：	Rumba Plan
 * 
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2007，所有权利保留。
 * 
 * 文件名：	RB_v20r00.java
 * 模块说明：	
 * 修改历史：
 * 2007-3-7 - zyb - 创建。
 */
package com.hd123.rumba.upgrade.v20.r01;

import net.sf.hibernate.HibernateException;

import org.apache.log4j.Logger;

import com.hd123.rumba.action.qt.ViewQuery;
import com.hd123.rumba.cartridge.CartRumba;
import com.hd123.rumba.cartridge.biz.IBCartridgeMgr;
import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.runtime.NamingMgr;
import com.hd123.rumba.upgrade.DBCommand;
import com.hd123.rumba.upgrade.Upgrade;
import com.hd123.rumba.user.IPermissionMgr;
import com.hd123.rumba.util.Version;

/**
 * @author zyb
 * 
 */
public class RB_v20r00 extends Upgrade {

  public RB_v20r00() throws Exception {
    super();
    baseVersion = new Version("2.0.0.0");
    targetVersion = new Version("2.0.1.0");
    cartUuid = this.getCart(CartRumba.class).getUuid();
    logger = Logger.getLogger(this.getClass());
  }

  @Override
  public void executeDdl() throws BusinessException {
    DBCommand cmd = new DBCommand();
    execFile(cmd, "upgrade.sql");
  }

  @Override
  public void executeData() throws BusinessException {
    // 增加FVRumbaQt(查询工具功能视图)
    logger.info("升级" + this.getCart(CartRumba.class).getCaption());
    IBCartridgeMgr cartMgr = (IBCartridgeMgr) NamingMgr.getInstance().lookupMgr(
        IBCartridgeMgr.class);
    try {
      cartMgr.saveCartridgeForInstallT(this.getCart(CartRumba.class).getUuid(), this.baseVersion);
      initPermissions();
    } catch (HibernateException e) {
      throw new BusinessException(e);
    }
  }

  private void initPermissions() throws HibernateException, BusinessException {
    IPermissionMgr permMgr = (IPermissionMgr) NamingMgr.getInstance().lookupMgr(
        IPermissionMgr.class);
    try {
      permMgr.initPermissionsT(ViewQuery.class);
    } catch (Exception e) {
      if (e instanceof HibernateException)
        throw (HibernateException) e;
      else
        throw new BusinessException(e);
    }
  }
}
